import { Command } from '../constant';
import Taro from '@tarojs/taro';
import { Dispatch } from 'typings';
import { IAllReducerProps } from '../types';
import { getReducerData } from '@/redux/store';
import { extraPathsValue } from '@/redux/util';
import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    async page() {
      let params = {};
      let itemStr = Taro.getStorageSync('b2b-wechat@login');
      let selectDistance = Taro.getStorageSync('selectDistance');
      if (itemStr) {
        let userInfo = itemStr.user
        params = {
          unionId: userInfo.unionId,
          iconName: '',
          communityId: selectDistance.id,
        }
      } else {
        params = {
          unionId: '',
          iconName: '',
          communityId: selectDistance.id,
        }
      }
      let res = await api.pagesIndexController.getMegaDataH5(params);
      if (res.success) {
        action.commonChange('main.ascData', res.data);
      }
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageAGSMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
